//==============================================================================
//
//               Copyright (C) 2002  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2004-02-01 08:43:01 $ $Revision: 1.2 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DDbm.h"

#include "DInc.h"
#include "DTest.h"

//-Wrappers--------------------------------------------------------------------


void DDbm_test(void)
{
#ifdef HAVE_DDBM
  DDbm  *dbm = [DDbm alloc];
  
  STARTTEST();

  [dbm init :"dbm" :"n"];
  TEST([dbm isOpen]);
  TEST(![dbm isReadOnly]);
  
  if ([dbm isOpen])
  {
    long key = 1;
    long val = 1234;
    
    TEST([dbm insert :"01-01-2004" :10 :"New years day" :13]);
    TEST([dbm insert :"25-01-2004" :10 :"Christmas"     :9] );
    TEST([dbm insert :"30-04-2004" :10 :"Queens day"    :10]);
    
    TEST([dbm insert :&key :sizeof(key) :&val :sizeof(val)]);
    
    val = 5678;
    TEST([dbm insert :&key :sizeof(key) :&val :sizeof(val)]);
    
    TEST([dbm has    :"01-01-2004" :10]);
    
    TEST([dbm get    :"25-01-2004" :10] != nil);
    
    TEST(![dbm delete :"25-02-2004" :10]);
    
    TEST([dbm delete :"25-01-2004" :10]);
  }
  
  [dbm close];
  
  TEST([dbm open :"dbm" :"r"]);
  TEST([dbm isOpen]);
  TEST([dbm isReadOnly]);
  
  if ([dbm isOpen])
  {
    DData *data;
    DList *list;
    long   key = 1;
    
    TEST([dbm has    :"01-01-2004" :10]);
    
    data = [dbm get  :"30-04-2004" :10];
    
    TEST(data != nil);
    
    TEST([data bcompare :"Queens day" :10] == 0);
    
    [data free];

    TEST(![dbm has   :"25-01-2004" :10]);
    
    TEST(![dbm delete :"30-04-2004" :10]); 
    TEST([dbm error] == 11);                  // GDBM_READER_CANT_DELETE
    
    data = [dbm get :&key :sizeof(key)];
    
    TEST(data != nil);
    
    if (data != nil)
      TEST(* (long *) [data data] == 5678);
    
    [data free];

    list = [dbm keys];
    
    TEST(list != nil);
    TEST([list length] == 3);
    
    [list free];
    
    list = [dbm objects];
    
    TEST(list != nil);
    TEST([list length] == 3);
    
    [list free];
  }
  
  [dbm free];
    
  STOPTEST();
#endif
}

